package com.uwo.commons.configuration.aop.validate.validate;

import com.uwo.commons.configuration.aop.validate.ValidateField;
import com.uwo.commons.configuration.aop.validate.ValidateProperties;
import com.uwo.commons.configuration.aop.validate.annotation.Number;
import com.uwo.commons.configuration.aop.validate.ErrorCode;
import com.uwo.commons.configuration.aop.validate.excepteion.ApiValidateExceptrion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 格式验证
 * @author hao.yan
 */
@Component(value = "Validate_Number")
public class NumberValidate extends NotNullValidate{

    @Autowired
    private ValidateProperties validateProperties;

    @Override
    public void validate(Annotation annotation, ValidateField validateField) throws ApiValidateExceptrion {
        super.validate(annotation, validateField);

        Number number = (Number)annotation;

        long num = Long.valueOf(validateField.getValue().toString());
        if(number.min() > num){
            // 属性配置
            String message = value(validateProperties.getMessages(), number.message(),
                    ErrorCode.LESS_THAN.getMessage(validateField.getName(), number.min()));
            exception(number.code(), message, ErrorCode.LESS_THAN);
        }
        if (num > number.max()){
            // 属性配置
            String message = value(validateProperties.getMessages(), number.message(),
                    ErrorCode.GREATER_THAN.getMessage(validateField.getName(), number.min()));
            exception(number.code(), message, ErrorCode.GREATER_THAN);
        }
    }

}
